package com.gcs.demo.filter;

import com.gcs.demo.servlet.CustomRequestWrapper;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
@WebFilter(filterName = "channelFilter",urlPatterns = {"/*"})
public class CustomFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;
        if(request instanceof HttpServletRequest){
            requestWrapper = new CustomRequestWrapper((HttpServletRequest) request);
        }
        if(requestWrapper == null){
            filterChain.doFilter(request,servletResponse);
        }else{
            filterChain.doFilter(requestWrapper,servletResponse);
        }
    }
}
